package com.tanhua.dubbo.server.api;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.Users;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * @author feihao
 * @version 1.0
 * @date 2021/7/6 10:43
 */

@Slf4j
@Service(version = "1.0.0")
public class UsersApiImpl implements UsersApi {


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 添加好友
     *
     * @param userId   自己用户的id
     * @param friendId 要添加的好友 id
     * @return
     */
    @Override
    public Boolean addFriends(Long userId, Long friendId) {
        //首先查询两人是否为好友：
        Query query = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count = mongoTemplate.count(query, Users.class);
        if (count > 0) {
            //有好友关系存在，返回false
            return false;
        }
        //添加好友关系：
        Users users = new Users();
        users.setId(new ObjectId());
        users.setUserId(userId);
        users.setFriendId(friendId);
        users.setDate(System.currentTimeMillis());
        //添加mongo数据库：
        Users insert1 = mongoTemplate.insert(users);

        //好友方添加：
        users.setId(new ObjectId());
        users.setUserId(friendId);
        users.setFriendId(userId);
        users.setDate(System.currentTimeMillis());
        Users insert2 = mongoTemplate.save(users);


        return ObjectUtil.isAllNotEmpty(insert1, insert2);
    }

    /**
     * 删除好友
     *
     * @param userId
     * @param friendId
     * @return
     */
    @Override
    public Boolean removeFriends(Long userId, Long friendId) {
        //首先查询两人是否为好友：
        Query query1 = Query.query(Criteria.where("userId").is(userId).and("friendId").is(friendId));
        long count1 = mongoTemplate.count(query1, Users.class);
        Query query2 = Query.query(Criteria.where("userId").is(friendId).and("friendId").is(userId));
        long count2 = mongoTemplate.count(query2, Users.class);
        if (count1 == 0 && count2 == 0) {
            //没有好友关系存在，返回false
            return false;
        }

        //删除我和好友的数据：
        long deletedCount1 = mongoTemplate.remove(query1, Users.class).getDeletedCount();
        //删除好友与我关系：
        long deletedCount2 = mongoTemplate.remove(query2, Users.class).getDeletedCount();
        return deletedCount1 > 0 && deletedCount2 > 0;
    }

    @Override
    public List<Users> queryAllUsers(Long id, String keyword) {  //关键字数据库没有设计，没用
        Query query = Query.query(Criteria.where("userId").is(id));
        return mongoTemplate.find(query, Users.class);
    }

    /**
     * 查询好友列表
     * @param id
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Users> queryUsersByPage(Long id, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("date")));
        Query query = Query.query(Criteria.where("userId").is(id)).with(pageRequest);
        List<Users> usersList = mongoTemplate.find(query, Users.class);

        PageInfo<Users> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(usersList);
        return pageInfo;
    }
}
